/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 * Supplier and Fireplace Reports, Login, main menu and Tutorial.
 * 
 * It also Includes Database connection, Database Extension and the two classes which are MySupplier and My Fireplace.
 *  
 * Copyright (C) Bournemouth University 
 * 
 * @author Ruth Shepperd, Ari Lopez
 * 
 * @version 1.0
 */
package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.EtchedBorder;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import java.awt.Font;
import javax.swing.SwingConstants;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import Database.DatabaseExtended;
import Database.MyFireplace;
import Database.MySupplier;

public class SupplierUpdate extends JFrame {

	// Attributes
	private String id;
	private JPanel contentPane;
	private JTextField emailTextField;
	private JTextField townTextField;
	private JTextField postcodeTextField;
	private JTextField telephoneNoTextField;
	private JTextField sNameTextField;
	private JButton searchButton;
	private JButton deleteButton;
	private JTextArea addressTextArea;
	private JButton saveButton;
	JComboBox<MySupplier> sSearchComboBox;
	ComboBoxModel<MySupplier> cModel;
	private MySupplier supplier;
	private MySupplier[] s;
	private String SupplierID;
	DefaultComboBoxModel<MySupplier> model;
	private JFrame thisFrame = this;
	private JButton exitButton;

	/**
	 * Constructor method to set all of the supplier details
	 * 
	 * @param supplier
	 *            (My Supplier)
	 */
	public SupplierUpdate(MySupplier supplier) {
		this();
		this.supplier = supplier;

		id = supplier.getSupplierID();
		sNameTextField.setText(supplier.getSupplierName());
		addressTextArea.setText(supplier.getAddress());
		townTextField.setText(supplier.getTown());
		postcodeTextField.setText(supplier.getPostcode());
		telephoneNoTextField.setText(supplier.getTelephoneNo());
		emailTextField.setText(supplier.getEmail());
	}

	/**
	 * Create the frame.
	 */
	public SupplierUpdate() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 431, 566);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setResizable(false);

		// Supplier Search ComboBox
		sSearchComboBox = new JComboBox<MySupplier>();

		MySupplier suppliers[] = DatabaseExtended.getSuppliers();
		model = new DefaultComboBoxModel<MySupplier>();

		// System.out.println( "Supplier Size: " + suppliers.length);
		for (MySupplier s : suppliers) {
			model.addElement(s);
			// System.out.println(s.toString());
		}
		sSearchComboBox.setModel(model);

		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JLabel label_1 = new JLabel("Update Supplier");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(gl_panel_2.createParallelGroup(
				Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				gl_panel_2
						.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 375,
								GroupLayout.PREFERRED_SIZE).addContainerGap()));
		gl_panel_2.setVerticalGroup(gl_panel_2.createParallelGroup(
				Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				gl_panel_2
						.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 37,
								GroupLayout.PREFERRED_SIZE).addContainerGap()));
		panel_2.setLayout(gl_panel_2);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		JLabel label = new JLabel("Search Supplier:");

		// Search Button
		searchButton = new JButton("Select");
		searchButton.addActionListener(new ActionListener() {
			/**
			 * Set all of the details for a particular supplier
			 */
			public void actionPerformed(ActionEvent arg0) {

				// Select the fireplace from the comobobox
				MySupplier supplier = (MySupplier) sSearchComboBox
						.getSelectedItem();
				// Printing for debugging purposes
				// System.out.println("This is the supplier ID from the combobox"
				// + supplier.getSupplierID());
				// set textfields with the data of the fireplace selected
				// in order to enable the user to update it
				sNameTextField.setText(supplier.getSupplierName());
				addressTextArea.setText(supplier.getAddress());
				townTextField.setText(supplier.getTown());
				postcodeTextField.setText(supplier.getPostcode());
				telephoneNoTextField.setText(supplier.getTelephoneNo());
				emailTextField.setText(supplier.getEmail());
			}
		});

		// Delete Button
		deleteButton = new JButton("Delete");
		deleteButton.addActionListener(new ActionListener() {
			/**
			 * Set all of the details ready to be deleted
			 */
			public void actionPerformed(ActionEvent arg0) {
				// set up variable that contains warning dialogue
				int reply = JOptionPane.showConfirmDialog(null,
						"Are you sure that you want to Delete?", "Delete",
						JOptionPane.YES_NO_OPTION);
				// if selected yes option....
				if (reply == JOptionPane.YES_OPTION) {

					MySupplier supplier = (MySupplier) sSearchComboBox
							.getSelectedItem();
					// print data for debugging purposes
					// System.out.println("This is the supplier ID from the combobox"
					// + supplier.getSupplierID());
					// replace /n that could be interfering
					supplier.setSupplierID(supplier.getSupplierID()
							.replaceAll("/n", "").trim());
					// and clear fields
					sNameTextField.setText("");
					addressTextArea.setText("");
					townTextField.setText("");
					postcodeTextField.setText("");
					telephoneNoTextField.setText("");
					emailTextField.setText("");

					// Delete the supplier
					DatabaseExtended.deleteSupplier(supplier);
					// remove item from the combobox when supplier deleted
					sSearchComboBox.removeItem(sSearchComboBox
							.getSelectedItem());
				}
			}
		});

		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1
				.setHorizontalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 379, Short.MAX_VALUE)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(label)
										.addGap(50)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addGroup(
																gl_panel_1
																		.createSequentialGroup()
																		.addComponent(
																				searchButton)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				deleteButton))
														.addComponent(
																sSearchComboBox,
																GroupLayout.PREFERRED_SIZE,
																175,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(62, Short.MAX_VALUE)));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 89, Short.MAX_VALUE)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label)
														.addComponent(
																sSearchComboBox,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																searchButton,
																GroupLayout.DEFAULT_SIZE,
																32,
																Short.MAX_VALUE)
														.addComponent(
																deleteButton,
																GroupLayout.PREFERRED_SIZE,
																31,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		panel_1.setLayout(gl_panel_1);

		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		JLabel label_2 = new JLabel("Telephone:");

		JLabel label_3 = new JLabel("Postcode:");

		JLabel label_4 = new JLabel("Email:");

		JLabel label_5 = new JLabel("Town:");

		JLabel label_6 = new JLabel("Address:");

		JLabel label_7 = new JLabel("Name:");

		addressTextArea = new JTextArea();
		addressTextArea.setWrapStyleWord(true);
		addressTextArea.setTabSize(0);
		addressTextArea.setLineWrap(true);

		emailTextField = new JTextField();
		emailTextField.setColumns(10);

		townTextField = new JTextField();
		townTextField.setColumns(10);

		postcodeTextField = new JTextField();
		postcodeTextField.setColumns(10);

		telephoneNoTextField = new JTextField();
		telephoneNoTextField.setColumns(10);

		sNameTextField = new JTextField();
		sNameTextField.setColumns(10);
		GroupLayout gl_panel_3 = new GroupLayout(panel_3);
		gl_panel_3
				.setHorizontalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 272, Short.MAX_VALUE)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(label_2)
														.addComponent(label_3)
														.addComponent(label_4)
														.addComponent(label_5)
														.addComponent(label_6)
														.addComponent(label_7))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																addressTextArea)
														.addComponent(
																emailTextField,
																GroupLayout.DEFAULT_SIZE,
																176,
																Short.MAX_VALUE)
														.addGroup(
																gl_panel_3
																		.createParallelGroup(
																				Alignment.TRAILING,
																				false)
																		.addComponent(
																				townTextField,
																				Alignment.LEADING)
																		.addComponent(
																				postcodeTextField,
																				Alignment.LEADING)
																		.addComponent(
																				telephoneNoTextField,
																				Alignment.LEADING,
																				GroupLayout.DEFAULT_SIZE,
																				118,
																				Short.MAX_VALUE))
														.addComponent(
																sNameTextField,
																GroupLayout.DEFAULT_SIZE,
																176,
																Short.MAX_VALUE))
										.addContainerGap()));
		gl_panel_3
				.setVerticalGroup(gl_panel_3
						.createParallelGroup(Alignment.LEADING)
						.addGap(0, 263, Short.MAX_VALUE)
						.addGroup(
								gl_panel_3
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_7)
														.addComponent(
																sNameTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(label_6)
														.addComponent(
																addressTextArea,
																GroupLayout.PREFERRED_SIZE,
																44,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_5)
														.addComponent(
																townTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_3)
														.addComponent(
																postcodeTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_2)
														.addComponent(
																telephoneNoTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(18)
										.addGroup(
												gl_panel_3
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(label_4)
														.addComponent(
																emailTextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(14, Short.MAX_VALUE)));
		panel_3.setLayout(gl_panel_3);

		// Save button
		saveButton = new JButton("Save and Submit");
		saveButton.addActionListener(new ActionListener() {
			/**
			 * Get the data of all details
			 */
			public void actionPerformed(ActionEvent arg0) {

				// Set up new array that will contain the input data of the
				// screen
				String[] data = new String[7];
				// Set up a supplier object that will contain the suppliers from
				// the database
				MySupplier supplier = (MySupplier) sSearchComboBox
						.getSelectedItem();
				// priting for debug purposes
				// System.out.println("This is the supplier ID from the combobox"
				// + supplier.getSupplierID());
				// if id is not null set up to null
				try {
					data[0] = supplier.getSupplierID();
				} catch (Exception e) {
					data[0] = null;
				}
				// get the data from the screen fields and put in the array.
				data[1] = sNameTextField.getText();
				data[2] = addressTextArea.getText();
				data[3] = townTextField.getText();
				data[4] = postcodeTextField.getText();
				data[5] = telephoneNoTextField.getText();
				data[6] = emailTextField.getText();

				// Save and Update supplier
				DatabaseExtended.updateSupplier(new MySupplier(data));
				// dispose the frame after the update
				thisFrame.dispose();
			}
		});

		// Main Menu Button
		exitButton = new JButton("Main Menu");
		exitButton.addActionListener(new ActionListener() {
			/**
			 * Dispose the screen
			 */
			public void actionPerformed(ActionEvent arg0) {
				thisFrame.dispose();
			}
		});

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.TRAILING)
														.addGroup(
																Alignment.LEADING,
																gl_contentPane
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				panel_1,
																				GroupLayout.DEFAULT_SIZE,
																				399,
																				Short.MAX_VALUE))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(10)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addGroup(
																								gl_contentPane
																										.createSequentialGroup()
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												panel_2,
																												GroupLayout.PREFERRED_SIZE,
																												399,
																												GroupLayout.PREFERRED_SIZE))
																						.addGroup(
																								gl_contentPane
																										.createSequentialGroup()
																										.addPreferredGap(
																												ComponentPlacement.RELATED)
																										.addComponent(
																												saveButton,
																												GroupLayout.PREFERRED_SIZE,
																												156,
																												GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												77,
																												Short.MAX_VALUE)
																										.addComponent(
																												exitButton,
																												GroupLayout.PREFERRED_SIZE,
																												166,
																												GroupLayout.PREFERRED_SIZE))
																						.addComponent(
																								panel_3,
																								Alignment.TRAILING,
																								GroupLayout.PREFERRED_SIZE,
																								389,
																								Short.MAX_VALUE))))
										.addGap(418)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(panel_2,
												GroupLayout.PREFERRED_SIZE, 63,
												GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addComponent(panel_1,
												GroupLayout.PREFERRED_SIZE, 89,
												GroupLayout.PREFERRED_SIZE)
										.addGap(18)
										.addComponent(panel_3,
												GroupLayout.PREFERRED_SIZE,
												263, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												ComponentPlacement.RELATED, 31,
												Short.MAX_VALUE)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.TRAILING)
														.addComponent(
																exitButton,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																saveButton,
																GroupLayout.PREFERRED_SIZE,
																31,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		contentPane.setLayout(gl_contentPane);
	}
}
